package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Case;
import com.hxht.cmp.entity.CourtRoom;
import com.hxht.cmp.entity.Plan;
import com.hxht.cmp.mapper.CaseMapper;
import com.hxht.cmp.mapper.PlanMapper;
import com.hxht.cmp.pojo.SearchCriteria;
import com.hxht.cmp.service.JudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JudgeServiceImpl implements JudgeService {
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private CaseMapper caseMapper;

    @Override
    public List<Map<String, Object>> selectJudgeCasePlanOrReview(SearchCriteria searchCriteria) {
        List<Plan> planList = null;
        if (searchCriteria.getReview().equals("0")) {
            //查询排期
            planList = planMapper.selectJudgeCasePlan(searchCriteria);
        } else {
            //查询回顾
            planList = planMapper.selectJudgeCaseReview(searchCriteria);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (!planList.isEmpty() && planList != null) {
            List<Case> caseList = caseMapper.selectCaseList(planList);
            for (Plan plan : planList) {
                for (Case aCase : caseList) {
                    if (plan.getCaseUid().equals(aCase.getUid())) {
                        Map<String, Object> map = new HashMap<>();
                        CourtRoom courtRoom = Constants.COURTROOM_MAP.get(plan.getCourtRoomUid());
                        map.put("courtId", plan.getCourtUid());
                        map.put("name", aCase.getName());
                        map.put("accuse", aCase.getAccuse());
                        map.put("accused", aCase.getAccused());
                        map.put("accuse", aCase.getType());
                        map.put("courtroomName", courtRoom.getName());
                        map.put("caseUid", aCase.getUid());
                        map.put("planUid", plan.getUid());
                        map.put("startDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(plan.getStartDate()));
                        if (plan.getStatus() == 0) {
                            map.put("statusName", "排期");
                        } else if (plan.getStatus() == 1) {
                            map.put("statusName", "正在开庭");
                        } else if (plan.getStatus() == 2) {
                            map.put("statusName", "休庭");
                        } else if (plan.getStatus() == 3) {
                            map.put("statusName", "闭庭");
                        } else if (plan.getStatus() == 4) {
                            map.put("statusName", "归档");
                        }
                        mapList.add(map);
                    }
                }
            }
        }
        return mapList;
    }

    @Override
    public Map<String, Object> selectMoreCasePlanOrReview(SearchCriteria searchCriteria) {
        //设置分页
        searchCriteria.setPageStart((searchCriteria.getPage() - 1) * searchCriteria.getPageSize());
        List<Map<String, Object>> listMap=null;
        int count=0;
        if (searchCriteria.getReview().equals("0")) {
            //更多开庭
            count= planMapper.getCountMoreCasePlan(searchCriteria);
            listMap= planMapper.selectMoreCasePlan(searchCriteria);
        } else {
            //更多回顾
            count= planMapper.getCountMoreCaseReview(searchCriteria);
            listMap= planMapper.selectMoreCaseReview(searchCriteria);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> map : listMap) {
            map.put("courtroomName", Constants.COURTROOM_MAP.get(map.get("courtroomId")).getName());
            map.put("caseTypeName", Constants.CASE_TYPE_MAP.get(map.get("caseTypeId")).getName());
            map.put("startDate", format.format(map.get("startDate")));
            map.put("lastUpdated", format.format(map.get("lastUpdated")));
        }
        Map<String,Object> map =new HashMap<>();
        map.put("count",count);
        map.put("data",listMap);
        return map;
    }
}
